package com.jinhao.crowd.mvc.handler;

import com.jinhao.crowd.entity.Auth;
import com.jinhao.crowd.service.api.AuthService;
import com.jinhao.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created on 2021/10/3.
 *
 * @author Valar Morghulis
 */

@Controller
public class AuthHandler {

    @Autowired
    private AuthService authService;

    /**
     * 重新保存角色-权限关系
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping("/assign/save/role/auth/relationship.json")
    public ResultEntity<String> saveRoleAuthRelation(
            @RequestBody Map<String, List<Integer>> map) {

        authService.updateRoleAuthRelationship(map);
        return ResultEntity.successWithoutData();
    }

    /**
     * 通过roleId在inner_role_auth表中查询到authid的集合
     *
     * @param roleId
     * @return
     */
    @ResponseBody
    @RequestMapping("/assign/get/assign/auth/id/by/role/id.json")
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(
            @RequestParam("roleId") Integer roleId) {
        List<Integer> authIdList = authService.getAssignedAuthIdByRoleId(roleId);
        return ResultEntity.successWithData(authIdList);
    }

    /**
     * 获取auth表中的所有数据，以JSON返回，通过设置ztree，将其自动包装到ztree中，省去手动包装
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/assign/get/all/auth.json")
    public ResultEntity<List<Auth>> getAuthTree() {

        List<Auth> authList = authService.getAll();

        return ResultEntity.successWithData(authList);
    }
}
